package cn.wildfire.chat.app.extendsion.classes;

import android.content.Context;
import android.util.Log;

import com.sourcemuch.im.caiba.wxapi.WXToken;
import com.sourcemuch.im.caiba.wxapi.WXUserInfo;

import java.util.HashMap;

import cn.wildfire.chat.app.Config;
import cn.wildfire.chat.app.extendsion.https.OkHttpRequest;
import cn.wildfire.chat.app.extendsion.https.ReqCallBack;
import cn.wildfire.chat.app.extendsion.utils.TextUtils;
import cn.wildfirechat.remote.ChatManager;

/**
 * Create by HEKE, 2019/10/14 11:31
 * TODO 获取微信用户信息
 */
public class WeiXinUserInfoRequest {

    private static final String token_url = "https://api.weixin.qq.com/sns/oauth2/access_token";
    private static final String user_url = "https://api.weixin.qq.com/sns/userinfo";

    private Context context;
    private String code;

    /**
     * 用户授权结果信息对象
     */
    protected WXToken wxToken;

    /**
     * 微信授权后账户信息对象
     */
    protected WXUserInfo wxUserInfo;

    private OnSourceMuchWXAuthListener onSourceMuchWXAuthListener;

    public WeiXinUserInfoRequest(Context context, String code, OnSourceMuchWXAuthListener onSourceMuchWXAuthListener) {
        this.context = context;
        this.code = code;
        this.onSourceMuchWXAuthListener = onSourceMuchWXAuthListener;
        req();
    }

    protected void req() {
        try {
            HashMap<String, String> params = new HashMap<>();
            params.put("appid", Config.WXAppID);
            params.put("secret", Config.WXAppSecret);
            params.put("code", code);
            params.put("grant_type", "authorization_code");
            OkHttpRequest.getNewInstance(context).requestAsyn(token_url, OkHttpRequest.TYPE_GET,
                    params, new ReqCallBack<Object>() {
                        @Override
                        public void onReqSuccess(Object result) {
                            try {
                                String ret = result.toString();
                                if (!TextUtils.isTextEmpty(ret)) {
                                    wxToken = (WXToken) new WXToken().toGsonObject(ret, WXToken.class);
                                    reqInfo();
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onReqFailed(String errorMsg) {
                            Log.e("### Get WX Token", " Error : " + errorMsg);
                            if (onSourceMuchWXAuthListener != null) {
                                onSourceMuchWXAuthListener.result(null, null, -2);
                            }
                        }
                    }).request();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void reqInfo() {
        try {
            HashMap<String, String> params = new HashMap<>();
            params.put("access_token", wxToken.access_token);
            params.put("openid", wxToken.openid);
            OkHttpRequest.getNewInstance(context).requestAsyn(user_url, OkHttpRequest.TYPE_GET,
                    params, new ReqCallBack<Object>() {
                        @Override
                        public void onReqSuccess(Object result) {
                            try {
                                String ret = result.toString();
                                if (!TextUtils.isTextEmpty(ret)) {
                                    wxUserInfo = (WXUserInfo) new WXUserInfo().toGsonObject(ret, WXUserInfo.class);
                                    wxUserInfo.clientId = ChatManager.Instance().getClientId();
                                    if (onSourceMuchWXAuthListener != null) {
                                        onSourceMuchWXAuthListener.result(wxToken, wxUserInfo, 0);
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onReqFailed(String errorMsg) {
                            Log.e("### Get WX UserInfo", " Error : " + errorMsg);
                            if (onSourceMuchWXAuthListener != null) {
                                onSourceMuchWXAuthListener.result(wxToken, null, -1);
                            }
                        }
                    }).request();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create by HEKE, 2019/10/14 14:23
     * TODO 回调微信授权登录信息
     */
    public interface OnSourceMuchWXAuthListener {
        void result(WXToken wxToken, WXUserInfo wxUserInfo, int req_error);
    }

}
